package com.ts.proj.controller;

//import com.lowagie.text.pdf.BaseFont;
import com.ts.base.controller.BaseController;
//import com.ts.proj.service.MeasureService;
import com.ts.proj.service.TemplatePageService;
//import com.ts.proj.vo.ReportVO;
import com.ts.util.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
//import org.xhtmlrenderer.pdf.ITextFontResolver;
//import org.xhtmlrenderer.pdf.ITextRenderer;
//
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;

@Controller
public class ReportController extends BaseController {


    @Autowired
    private TemplatePageService templatePageService;

    /**
     * 益隆进厂煤日报
     * @return
     */
    @RequestMapping("/business-{rout}")
    public ModelAndView businessYlInDaily(@PathVariable("rout") String rout){
        ModelAndView modelAndView = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){

            viewName = "business/report/"+rout;

        }
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    @RequestMapping(value = "/freemarker/{id}/{type}")
    @ResponseBody
    public String send(@PathVariable("id") Integer id,@PathVariable("type") String type)  {
        String result = "error";
        try {
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFCellStyle style = workbook.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFSheet sheet = workbook.createSheet("sheet");


            HSSFRow row0 = sheet.createRow(0);
            HSSFCell cell_00 = row0.createCell(0);
            cell_00.setCellStyle(style);
            cell_00.setCellValue("日期");
            HSSFCell cell_01 = row0.createCell(1);
            cell_01.setCellStyle(style);
            cell_01.setCellValue("午别");
            HSSFRow row1 = sheet.createRow(1);
            HSSFCell cell_10 = row1.createCell(0);
            cell_10.setCellStyle(style);
            cell_10.setCellValue("20180412");
            HSSFCell cell_11 = row1.createCell(1);
            cell_11.setCellStyle(style);
            cell_11.setCellValue("上午");

            HSSFRow row2 = sheet.createRow(2);
            HSSFCell cell_21 = row2.createCell(1);
            cell_21.setCellStyle(style);
            cell_21.setCellValue("下午");

            // 合并日期占两行(4个参数，分别为起始行，结束行，起始列，结束列)
            // 行和列都是从0开始计数，且起始结束都会合并
            // 这里是合并excel中日期的两行为一行
            CellRangeAddress region = new CellRangeAddress(1, 2, 0, 0);
            sheet.addMergedRegion(region);
            String fileName = DateUtil.getID();
            File file = new File("D:\\"+fileName+".xls");
            result = "下载路径为：D盘；下载文件名为：" + fileName + ".xls";
            FileOutputStream fout = new FileOutputStream(file);
            workbook.write(fout);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
